/*
 * IngresoDeNombre.cpp
 *
 *  Created on: 03/07/2014
 *      Author: loreyalan
 */

#include "IngresoDeNombre.h"

IngresoDeNombre::IngresoDeNombre(SDL_Surface *screen, SDL_Event *event, SDL_Color *textColor, TTF_Font *font){
    //Initialize the string
    str = "";

    /* Fuentes */
    this->font = font;

    /* Inicializo la pantalla principal */
    this->screen = screen;

    /* Inicializo la pantalla principal */
    this->event = event;

    this->textColor = textColor;

    //Initialize the surface
    text = NULL;

    //Enable Unicode
    SDL_EnableUNICODE( SDL_ENABLE );
}

IngresoDeNombre::~IngresoDeNombre(){
    //Free text surface
    SDL_FreeSurface( text );

    //Disable Unicode
    SDL_EnableUNICODE( SDL_DISABLE );
}

bool IngresoDeNombre::handle_input(){
	/* Se fija si toco escape */
	if(event->key.keysym.sym == SDLK_ESCAPE)
		return false;

    //If a key was pressed
    if( event->type == SDL_KEYDOWN )
    {
        //Keep a copy of the current version of the string
        std::string temp = str;

        //If the string less than maximum size
        if( str.length() < 4 )
        {
        	/*
            //If the key is a space
            if( event->key.keysym.unicode == (Uint16)' ' )
            {
                //Append the character
                str += (char)event->key.keysym.unicode;
            }
            //If the key is a number
            else if( ( event->key.keysym.unicode >= (Uint16)'0' ) && ( event->key.keysym.unicode <= (Uint16)'9' ) )
            {
                //Append the character
                str += (char)event->key.keysym.unicode;
            }

            //If the key is a uppercase letter
            else */

            if( ( event->key.keysym.unicode >= (Uint16)'A' ) && ( event->key.keysym.unicode <= (Uint16)'Z' ) )
            {
                //Append the character
                str += (char)event->key.keysym.unicode;
            }
            //If the key is a lowercase letter
            else if( ( event->key.keysym.unicode >= (Uint16)'a' ) && ( event->key.keysym.unicode <= (Uint16)'z' ) )
            {
                //Append the character
                str += (char)event->key.keysym.unicode;
            }
        }

        //If backspace was pressed and the string isn't blank
        if( ( event->key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
        {
            //Remove a character from the end
            str.erase( str.length() - 1 );
        }

        //If the string was changed
        if( str != temp )
        {
            //Free the old surface
            SDL_FreeSurface( text );

            //Render a new text surface
            text = TTF_RenderText_Solid( font, str.c_str(), *textColor );
        }
    }

    return true;
}

void IngresoDeNombre::show_centered()
{
    //If the surface isn't blank
    if( text != NULL ){
        //Show the name
        apply_surface( ( SCREEN_WIDTH - text->w ) / 2, ((SCREEN_HEIGHT-(text->h / 2))/ 5 )* 4, text, screen, NULL);
    }
}

void IngresoDeNombre::getNombre(char *destino){
	std::string::iterator it = this->str.begin();
	std::string::iterator it1 = this->str.end();
	char nombre[5] = {0};
	int x=0;

	while(x < 4 && it != it1){
		nombre[x] = (*it);
		it++;
		x++;
	}

	strcpy(destino, nombre);
}


void IngresoDeNombre::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip){
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}
